<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <!-- <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="代理商名称">
              <a-input placeholder="请输入代理商名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div> -->
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="openModal('add')" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="openModal('edit', record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a style="color: red;">删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <a-modal
      :title="title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      :width="900"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <vue-editor
        v-model="formData.content"
        useCustomImageHandler
        @image-added="handleImageAdded"
        :editor-toolbar="customToolbar"
      />
    </a-modal>
  </a-card>
</template>

<script>
import { VueEditor } from 'vue2-editor'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { apiGetReferenceList, apiAddAgent, apiEditAgent, apiDelAgent, apiDelAgentBatch } from '@/api/agentManage.js'
import { apiUpfile, apiEdit, apiAdd } from '@/api/contractManage/contractTemplate.js'

export default {
  name: 'AgentManageMaintenance',
  mixins: [JeecgListMixin, mixinDevice],
  components: { VueEditor },
  data() {
    return {
      description: '合同模板',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '模板内容',
          align: 'center',
          dataIndex: 'content'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/ContractTemplate/list',
        delete: '/ContractTemplate/delete',
        deleteBatch: '/ContractTemplate/deleteBatch'
      },
      title: '新增合同模板',
      modalType: 'add',
      visible: false,
      confirmLoading: false,
      formData: {
        content: ''
      },
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      content: '',
      customToolbar: [
        [{ header: [false, 1, 2, 3, 4, 5, 6] }],
        ['bold', 'italic', 'underline', 'strike'], // toggled buttons
        [{ align: ['', 'center', 'right', 'justify'] }],
        ['blockquote'],
        [{ list: 'ordered' }, { list: 'bullet' }, { list: 'check' }],
        [{ script: 'sub' }, { script: 'super' }], // superscript/subscript
        [{ indent: '-1' }, { indent: '+1' }], // outdent/indent
        [{ color: [] }, { background: [] }], // dropdown with defaults from theme
        ['link', 'image'],
        ['clean']
      ]
    }
  },
  methods: {
    openModal(type, row) {
      this.title = type === 'add' ? '新增合同模板' : '编辑合同模板'
      if (type === 'edit') {
        this.formData = Object.assign({}, row)
      }
      this.visible = true
    },
    async handleOk() {
      const apiFun = this.title === '新增合同模板' ? apiAdd : apiEdit
      this.confirmLoading = true
      const { code, message } = await apiFun(this.formData)
      if (code === 200) {
        this.$message.success(message)
        this.handleCancel()
        this.searchQuery()
      } else {
        this.$message.error(message)
      }
      this.confirmLoading = false
    },
    handleCancel() {
      this.formData = { content: '' }
      this.visible = false
    },
    handleImageAdded: function(file, Editor, cursorLocation, resetUploader) {
      var formData = new FormData()
      formData.append('file', file)
      apiUpfile(formData)
        .then(({ code, result }) => {
          if (code === 0) {
            Editor.insertEmbed(cursorLocation, 'image', result.fileUrl)
            resetUploader()
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
